/**
 * 保存违法行为
 * @param data
 * @param callback
 */
function saveCaseCauseOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_CAUSE (CASE_CAUSE_ID,CAUSENUMBER,ZFML,ZFMLID,CAUSECONTENT) " +
    			"VALUES (?,?,?,?,?)",
    			[data.id==undefined ? '' : data.id  ,data.strNumber==undefined ? '' : data.strNumber  ,
    			 data.zfml==undefined ? '' : data.zfml  ,data.zfmlId==undefined ? '' : data.zfmlId  ,
    			 data.strContent==undefined ? '' : data.strContent],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("违法行为保存失败");
	   });
    });
}
/**
 * 保存法条
 * @param data
 * @param callback
 */
function saveLawContentOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_LAW_CONTENT (CASE_LAW_CONTENT_ID,STRNAME,STRNUMBER,STRCODE,STRITEM,STRCLAUSE,YJTL,AMOUNT,UPPERLIMIT,LAWERLIMIT,CFBZ,STRIITEM,CASE_CAUSE_ID) " +
    			"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
    			[data.id==undefined ? '' : data.id  ,data.strName==undefined ? '' : data.strName  ,
				 data.bnumber==undefined ? '' : data.bnumber  , data.code==undefined ? '' : data.code  ,
    			 /*data.chapter==undefined ? '' : data.chapter  ,data.section==undefined ? '' : data.section  ,*/
				 data.item==undefined ? '' : data.item, data.clause==undefined ? '' : data.clause,
				 /*data.content==undefined ? '' : data.content,*/data.yjtl==undefined ? '' : data.yjtl,
				 data.amount==undefined ? '' : data.amount,
				 data.upperLimit==undefined ? '' : data.upperLimit  , data.lawerLimit==undefined ? '' : data.lawerLimit,
				 data.cfbz==undefined ? '' : data.cfbz,
				 data.iitem==undefined ? '' : data.iitem,data.caseCauseId==undefined ? '' : data.caseCauseId],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("法条保存失败");
	   });
    });
}

/**
 * 保存自由裁量权
 * @param data
 * @param callback
 */
function saveDiscretionOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_DISCRETION (CASE_DISCRETION_ID,WFCD,WFCDID,WFQJ,DCFBZ,UPPERLIMIT,LAWERLIMIT,CASE_CAUSE_ID) " +
    			"VALUES (?,?,?,?,?,?,?,?)",
    			[data.id==undefined ? '' : data.id  ,data.wfcd==undefined ? '' : data.wfcd  ,
    			 data.wfcdId==undefined ? '' : data.wfcdId  ,data.wfqj==undefined ? '' : data.wfqj  ,
    			 data.cfbz==undefined ? '' : data.cfbz,data.upperLimit==undefined ? '' : data.upperLimit  ,
				 data.lawerLimit==undefined ? '' : data.lawerLimit,data.caseCauseId==undefined ? '' : data.caseCauseId],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("自由裁量权保存失败");
	   });
    });
}

/**
 * 根据查询条件获取法条
 * @param condition 查询条件
 * @param page 分页
 * @param callback  回调函数
 **/
function findLawContentTotalOffLine(condition,con,page){
	var sql = addCondition(condition);
	db.transaction(function (tx) { 
		tx.executeSql("SELECT COUNT(*) TOTAL  FROM CASE_LAW_CONTENT CLC,CASE_CAUSE CC WHERE CC.CASE_CAUSE_ID=CLC.CASE_CAUSE_ID" + sql, [], 
		function (tx, result){
		    var len = result.rows.length;
		    for (var i = 0; i < len; i++){
		    	page.data.total = result.rows.item(i).TOTAL;
		    }
		    /**
			 * 回调函数
			 */
			if(typeof(page.callback) == 'function'){
				page.callback();
			}
			$(con.datagrid).datagrid({
				border:false,
				fitColumns:true,
				checkbox:true,
				fit:true,
				remoteSort:false,
				rownumbers:false,
				singleSelect:con.singleSelect,
				toolbar: con.datagridtoolbar,
				pagination:con,
				loadFilter:pagerFilter}).datagrid('loadData', page.data);
	    }, 
	    function(tx, error) {
	    	message("获取法条信息列表失败:"+ error.message);
        });  
	});
}

/**
 * 根据查询条件法条列表
 * @param condition 查询条件
 * @param page 分页
 **/
function findAllLawContentOffLine(condition,con,page){
	var sql = addCondition(condition);
	db.transaction(function (tx) { 
		tx.executeSql("SELECT CC.CASE_CAUSE_ID,CC.CAUSENUMBER,CC.CAUSECONTENT,CLC.STRNAME,CLC.STRCODE,CLC.YJTL,CLC.STRITEM,CLC.STRCLAUSE,CLC.STRIITEM,CLC.AMOUNT,CLC.LAWERLIMIT,CLC.UPPERLIMIT FROM CASE_LAW_CONTENT CLC,CASE_CAUSE CC WHERE CC.CASE_CAUSE_ID=CLC.CASE_CAUSE_ID" + sql +" ORDER BY CLC.STRCODE DESC LIMIT ?,?", [(page.pageNumber)*page.pageSize,page.pageSize], 
		function (tx, result){
		    page.data.rows = setLawContentDataOffLine(result);
		    findLawContentTotalOffLine(condition,con,page);
	    }, 
	    function(tx, error) {
	    	message("获取法条信息列表失败:"+ error.message);
        });  
	});
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setLawContentDataOffLine(result){
	var len = result.rows.length;
    var data = new Array();
	for (var i = 0; i < len; i++){
		var lawContent = {};
		lawContent.caseCauseId = result.rows.item(i).CASE_CAUSE_ID;
		lawContent.causeNumber = result.rows.item(i).CAUSENUMBER;
		lawContent.causeContent = result.rows.item(i).CAUSECONTENT;
		lawContent.code = result.rows.item(i).STRCODE;
		lawContent.strName = result.rows.item(i).STRNAME;
		lawContent.yjtl   = result.rows.item(i).YJTL;
		/*lawContent.Section = result.rows.item(i).STRSECTION;*/
		lawContent.Item = result.rows.item(i).STRITEM;
		lawContent.Clause = result.rows.item(i).STRCLAUSE;
		lawContent.iItem = result.rows.item(i).STRIITEM;
		lawContent.amount = result.rows.item(i).AMOUNT;
		lawContent.upperLimit = result.rows.item(i).UPPERLIMIT;
		lawContent.lawerLimit = result.rows.item(i).LAWERLIMIT;
		/*lawContent.Content = result.rows.item(i).STRCONTENT;*/
	    data.push(lawContent);
    }
	return data;
}

/**
 * 根据违法行为ID查询自由裁量权
 **/
function findDiscretionByCaseCauseIdOffLine(caseCauseId,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_DISCRETION WHERE CASE_CAUSE_ID=?", [parseInt(caseCauseId)], 
		function (tx, result){
		    var data = setDiscretionDataOffLine(result);
		    if(typeof(callback) == 'function'){
		    	callback(data);
		    }
	    }, 
	    function(tx, error) {
	    	message("获取自由裁量权失败:"+ error.message);
        });  
	});
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setDiscretionDataOffLine(result){
	var len = result.rows.length;
    var data = new Array();
	for (var i = 0; i < len; i++){
		var discretion = {};
		discretion.id = result.rows.item(i).CASE_DISCRETION_ID;
		discretion.wfcd = result.rows.item(i).WFCD;
		discretion.wfcdId = result.rows.item(i).WFCDID;
		discretion.wfqj = result.rows.item(i).WFQJ;
		discretion.cfbz = result.rows.item(i).DCFBZ;
		discretion.upperLimit   = result.rows.item(i).UPPERLIMIT;
		discretion.lawerLimit = result.rows.item(i).LAWERLIMIT;
	    data.push(discretion);
    }
	return data;
}

/**
 * 拼接sql语句
 * @param condition 查询条件
 **/
function addCondition(condition){
	var sqlWhere = "";
	
	/**
	 * 违法行为
	 **/
	
	
	if($.trim(condition.content).length != 0){
		sqlWhere += " AND CC.CAUSECONTENT LIKE '%" + condition.content + "%'";
	}
	
	/**
	 *  法律法规编码
	 **/
	if($.trim(condition.code).length != 0){
		sqlWhere += " AND CLC.STRCODE LIKE '%" + condition.code + "%'";
	}
	
	/**
	 *  法律法规名称
	 **/
	if($.trim(condition.bname).length != 0){
		sqlWhere += " AND CLC.STRNAME LIKE '%" + condition.bname + "%'";
	}

	/**
	 * 条
	 */
	if($.trim(condition.item).length != 0){
		sqlWhere += " AND CLC.STRITEM LIKE '%" + condition.item + "%'";
	}
	
	/**
	 * 款
	 */
	if($.trim(condition.clause).length != 0){
		sqlWhere += " AND CLC.STRCLAUSE LIKE '%" + condition.clause + "%'";
	}
	/**
	 * 项
	 */
	if($.trim(condition.iItem).length != 0){
		sqlWhere += " AND CLC.STRIITEM LIKE '%" + condition.iItem + "%'";
	}
	/**
	 * 执法门类
	 */
	if($.trim(condition.zfmlId).length != 0){
		sqlWhere += " AND CC.ZFMLID = '" + condition.zfmlId +"'" ;
	}
	return sqlWhere;
}

/**
 * 初始化法条
 * @param db
 * @param callback
 **/
function initLawContent(){
	lawContent(getLawContentData);
}

/**
 * 违法行为数据
 */
function caseCause(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_CAUSE', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data,function(index,item){
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除法条失败");
		}); 
	});
}

/**
 * 自由裁量权数据
 */
function discretion(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_DISCRETION', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data,function(index,item){
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除法条失败");
		}); 
	});
}

/**
 * 法条数据
 */
function lawContent(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_LAW_CONTENT', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data,function(index,item){
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除法条失败");
		}); 
	});
}

/**
 * 清空数据
 */
function deleteCaseCauseAll(data,callback){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_CAUSE', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data, function(i, item) {
					/**
					 * 添加到本地
					 **/
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除违法行为失败");
		}); 
	});
}

/**
 * 清空数据
 */
function deleteDiscretionAll(data,callback){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_DISCRETION', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data, function(i, item) {
					/**
					 * 添加到本地
					 **/
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除自由裁量权失败");
		}); 
	});
}

/**
 * 从服务器上获取
 **/
function getLawContentData(){
	$.ajax({
		type : "post",
		url : getContextPath() + "/lawContent/findByOrganId",
		dataType : "json",
		data : {organId:$.cookie("organId")},
		error : function(request) {
			message("获取法条失败");
		},
		success : function(data) {
			deleteCaseCauseAll(data.caseCause,saveCaseCauseOffLine);
			deleteDiscretionAll(data.discretion,saveDiscretionOffLine);
			$.each(data.lawContent, function(i, item) {
				/**
				 * 添加到本地
				 **/
				saveLawContentOffLine(item);
			});
			//message("获取法条成功");
		}
	});
}